package com.ttdevs.ttrss.ui;

import uk.co.senab.actionbarpulltorefresh.library.ActionBarPullToRefresh;
import uk.co.senab.actionbarpulltorefresh.library.PullToRefreshLayout;
import uk.co.senab.actionbarpulltorefresh.library.listeners.OnRefreshListener;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import com.ttdevs.ttrss.R;

public class ArticleDetailActivity extends Activity implements OnRefreshListener {
	
	public static final String KEY_URL = "keyurl";
	private PullToRefreshLayout mPullToRefreshLayout;
	private WebView mWebView;
	private String url ;

	@SuppressLint("SetJavaScriptEnabled")
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_webview);

		// Find WebView and get it ready to display pages
		mWebView = (WebView) findViewById(R.id.wvArticle);
		mWebView.getSettings().setJavaScriptEnabled(true);
		mWebView.setWebViewClient(new SampleWebViewClient());
		// mWebView.getSettings().setSupportZoom(true);
		mWebView.getSettings().setUseWideViewPort(true);
		mWebView.getSettings().setBlockNetworkImage(true);
		
		// Now find the PullToRefreshLayout and set it up
		mPullToRefreshLayout = (PullToRefreshLayout) findViewById(R.id.ptr_layout);
		ActionBarPullToRefresh.from(this).allChildrenArePullable().listener(this).setup(mPullToRefreshLayout);

		url = getIntent().getExtras().getString(KEY_URL);
		if(url == null || "".equals(url)){
			finish();
		}
		// Finally make the WebView load something...
		mWebView.loadUrl(url);
	}

	@Override
	public void onRefreshStarted(View view) {
		// Here we just reload the webview
		mWebView.reload();
	}

	private class SampleWebViewClient extends WebViewClient {

		@Override
		public boolean shouldOverrideUrlLoading(WebView view, String url) {
			// Return false so the WebView loads the url
			return false;
		}

		@Override
		public void onPageFinished(WebView view, String url) {
			super.onPageFinished(view, url);

			// If the PullToRefreshAttacher is refreshing, make it as complete
			if (mPullToRefreshLayout.isRefreshing()) {
				mPullToRefreshLayout.setRefreshComplete();
			}
		}
	}
}
